#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Contact info views
"""

from contact.models.userinfo import Contact
from contact.web.response import success_response, json_response


def save_contact(request):
    """Save contact view"""
    datas = request.DATAS
    owner = request.REQUEST['user']
    for data in datas:
        query = Contact.all()
        c = query.filter('owner =', owner).filter('username =', 
                                                  data['username']).get()
        if c is None:
            c = Contact()
            c.owner = owner
        c.save(data)
    return success_response()

def get_contact(request):
    """Get contact view"""
    owner = request.REQUEST['user']
    query = Contact.all()
    rs = query.filter('owner =', owner)
    contacts = [r.to_dict() for r in rs]
    return json_response(contacts)